* Programas auxiliares para "analises.do"

****************************************************************************** 
* 1: M�dias gerais
	capture program drop medias
	program define medias, sortpreserve eclass
		version 12.0
		syntax varlist(min=1 numeric) [fweight aweight iweight] [if] [in]
		
		* quietly...
			quietly {
				* para o IF/IN
					tempvar wi touse
					mark `touse' `if' `in'
					markout `touse' `varlist'
					preserve
					quietly keep if `touse'		
				* peso
					if "`weight'" == "" { 
					gen `wi' = 1 
					} 
					else { 
						gen double `wi' `exp'
					}
				* m�dias
					foreach var in `varlist' { 
						qui sum `var' [w=`wi']
						scalar mean_`var'=r(mean)
					} 
				* matriz de output
					matrix define medias_output = (0)
					foreach var in `varlist' { 
						matrix define medias_output = ( medias_output \ mean_`var')
						scalar drop mean_`var'
					} 
					matrix define medias_output=medias_output[2...,1...]
					matrix colnames medias_output = mean
					matrix rownames medias_output = `varlist'
			}
		* ereturn 
			ereturn matrix medias = medias_output 
		* output
			matrix list e(medias), f(%9.0g)
		
		* restore
			restore
	end
******************************************************************************


****************************************************************************** 
* 2: Quem recebe
	capture program drop rec
	program define rec, sortpreserve eclass
		version 12.0
		syntax varlist(min=1 numeric) [fweight aweight iweight] [if] [in]
		
		* quietly...
			quietly {
				* para o IF/IN
					tempvar wi touse dummy
					mark `touse' `if' `in'
					markout `touse' `varlist'
					preserve
					quietly keep if `touse'		
				* peso
					if "`weight'" == "" { 
					gen `wi' = 1 
					} 
					else { 
						gen double `wi' `exp'
					}
				* quem recebe 
					foreach var in `varlist' { 
						gen `dummy'=(`var'~=0)
						qui sum `dummy' [w=`wi']
						drop `dummy'
						scalar rec_`var'=100*r(mean)
					} 
				* matriz de output
					matrix define rec_output = (0)
					foreach var in `varlist' { 
						matrix define rec_output = ( rec_output \ rec_`var')
						scalar drop rec_`var'
					} 
					matrix define rec_output=rec_output[2...,1...]
					matrix colnames rec_output = rec%
					matrix rownames rec_output = `varlist'
			}
			
		* ereturn 
			ereturn matrix rec = rec_output 
		* output
			matrix list e(rec), f(%5.0g)
		
		* restore
			restore
	end
******************************************************************************		


****************************************************************************** 
* 3: M�dias condicionais
	capture program drop medcond
	program define medcond, sortpreserve eclass
		version 12.0
		syntax varlist(min=1 numeric) [fweight aweight iweight] [if] [in]
		
		* quietly...
			quietly {
				* para o IF/IN
					tempvar wi touse
					mark `touse' `if' `in'
					markout `touse' `varlist'
					preserve
					quietly keep if `touse'		
				* peso
					if "`weight'" == "" { 
					gen `wi' = 1 
					} 
					else { 
						gen double `wi' `exp'
					}
				* m�dias
					foreach var in `varlist' { 
						qui sum `var' [w=`wi'] if `var'~=0
						scalar mean_`var'=r(mean)
					} 
				* matriz de output
					matrix define medias_output = (0)
					foreach var in `varlist' { 
						matrix define medias_output = ( medias_output \ mean_`var')
						scalar drop mean_`var'
					} 
					matrix define medias_output=medias_output[2...,1...]
					matrix colnames medias_output = medcond
					matrix rownames medias_output = `varlist'
			}
		* ereturn 
			ereturn matrix medcond = medias_output 
		* output
			matrix list e(medcond), f(%9.0g)
		
		* restore
			restore
	end
******************************************************************************

******************************************************************************
* Decomposicao do Gini
*! version 1.0 22feb2010
*! Pedro Souza

capture program drop ginideco

program ginideco, sortpreserve  rclass
   version 10.1

	syntax varlist (min=2 numeric) [fweight aweight iweight] [if] [in], [hh(varname numeric) Details]
	

	tempname i sumgdr gini ccgdr numvar nv nom medrdpc serdpc nom1 nomes nomes1
	local i = 0
	
	tempvar gdr wi cpop cren cgdr touse
	gen double `gdr'=0

	mark `touse' `if' `in'
	markout `touse' `varlist'

	preserve
	quietly keep if `touse'
	

	if "`hh'" == "" {
		foreach var in `varlist' {
			local  i = `i'+1
			tempvar temp`i'
			quietly gen double `temp`i'' = cond(`var'~=., `var', 0)
			quietly replace `gdr'=`gdr'+`temp`i''
			scalar `numvar'=`i'
		} 
	}
	else { 
		foreach var in `varlist' {
			local  i = `i'+1
			tempvar t`i'
			quietly gen double `t`i'' = cond(`var'~=., `var', 0)
			scalar `numvar'=`i'
		}
		local `nv' = `numvar'
		forvalues x=1/``nv'' {
			tempvar temp`x'
			quietly egen double `temp`x''=sum(`t`x''/_N), by(`hh')
		}
		forvalues x=1/``nv'' {
			quietly replace `gdr'=`gdr'+`temp`x''
		}
	}


	if "`weight'" == "" { 
		gen `wi' = 1 
	} 
	else { 
		gen double `wi' `exp'
	}
		
	if "`details'" != "" { 
		quietly sum `gdr' [w=`wi'] if `touse'
		scalar `medrdpc'=r(mean)
		scalar `serdpc'=r(sd)/sqrt(r(sum_w))
		local `nv' = `numvar'
		 forvalues x=1/``nv'' { 
			quietly sum `temp`x'' [w=`wi'] if `touse'
			tempname med`x' se`x'
			scalar `med`x''=r(mean)
			scalar `se`x''=r(sd)/sqrt(r(sum_w))
		}		

		di ""
		di as text "* DESCRIPTIVES: INCOME PER CAPITA *"
		di as text ""
		di as text "Source" as text _column(20) "Mean" as text _column(30) "Std Error"
		di as result "Total" as result %10.2f _column(15) `medrdpc' as result _column(27) %10.2f `serdpc'
		local nomes `varlist'
		local `nv' = `numvar'
		forvalues x=1/``nv'' { 
			gettoken `nomes1' nomes: nomes, parse(": ")
			di as result "``nomes1''" as result %10.2f _column(15) `med`x'' as result _column(27) %10.2f `se`x''
		}
	}

	sort `gdr'
	quietly sum `gdr' [w=`wi'] if `touse' , meanonly
	quietly gen double `cpop' = sum(`wi')/r(sum_w) if `touse'
	scalar `sumgdr'=r(sum)
	quietly gen double `cren' = sum(`gdr'*`wi')/`sumgdr' if `touse'
	quietly gen double `cgdr'= (((`cpop'-`cpop'[_n-1])*`cpop'[_n-1])+(((`cpop'-`cpop'[_n-1])^2)/2))- ///
			(`cpop'-`cpop'[_n-1])*((`cren'+`cren'[_n-1])/2)  if `touse'
	quietly sum `cgdr' if `touse' , meanonly
	scalar `ccgdr'=2*r(sum)

	local `nv' = `numvar'
	forvalues  i=1/``nv'' { 
		quietly sum `temp`i'' [w = `wi'] if `touse' , meanonly
		tempname share`i' cc`i'
		scalar `share`i''=r(sum)/`sumgdr'
		tempvar r`i' c`i'
		quietly gen double `r`i''=sum(`temp`i''*`wi')/r(sum)  if `touse'
		quietly gen double `c`i''= (((`cpop'-`cpop'[_n-1])*`cpop'[_n-1])+(((`cpop'-`cpop'[_n-1])^2)/2))- ///
			(`cpop'-`cpop'[_n-1])*((`r`i''+`r`i''[_n-1])/2)  if `touse'
		sum `c`i'' if `touse', meanonly
		scalar `cc`i''=2*r(sum)

	} 

	di as text ""
	di as text "*** GINI DECOMPOSITION BY INCOME SOURCE ***"
	di as text ""
	di as text "GINI INDEX:" as result _column(15)  %12.8f `ccgdr'
	di as text ""
	di as text "Variables" as text _column(16) "Coef_Conc" as text _column(29) "Prop_Inc" as text _column(42) "C_C*P_I" ///
		as text _column(52) "% of Gini"
	local `nv' = `numvar'
	forvalues x=1/``nv'' {
		gettoken `nom' varlist: varlist, parse(": ")
		di as result "``nom''" ///
		as result _column(13) %12.6f `cc`x'' ///
		as result  %12.6f  `share`x'' ///
		as result  %12.6f `cc`x''*`share`x'' ///
		as result  %12.6f 100*(`cc`x''*`share`x'')/`ccgdr'
	}
	
	return scalar gini = `ccgdr'
	local `nv' = `numvar'
	forvalues x=1/``nv'' { 
		return scalar cc`x'=`cc`x''
		return scalar prop`x'=`share`x''
	}
	
	restore
end
******************************************************************************
